# coding:utf-8
import asyncio

from aiobsonrpc import (ClientConnect, BatchBuilder, service_class, request, notification)

HOST_PORT = ("127.0.0.1", 8000)


@service_class
class EchoClient(object):
    @request
    async def swapper(self, txt):
        return f"revers:{''.join(reversed(list(txt)))}"

    @notification
    async def report_progress(self, txt):
        print(f'Msg from server:{txt}')


async def main():
    # Cut-the-corners TCP Client:
    conn = ClientConnect(*HOST_PORT, services=EchoClient())
    await conn.run()

    rpc = conn.rpc

    # Call
    await rpc.invoke_notification('notify', 'client:main')
    server = rpc.get_peer_proxy()
    print("server:long_process:", await server.long_process(2, 3, 4))
    result = await server.swapper('Hello World!')
    print(result)

    # Batch call
    bb = BatchBuilder()
    bb.echo_times(100, 2)
    bb.echo_times(100, 3)
    batch_result = await rpc.batch_call(bb)
    print(batch_result)

    conn.writer.close()


if __name__ == '__main__':
    asyncio.run(main())
